﻿using NewsCollection.Common.Service;
using NewsCollection.Common.UpdateGUI;
using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsCollection.ListWeb
{
    class DanTri
    {
        ProcessDb pdb = new ProcessDb();
        UpdateButton updateBtn = new UpdateButton();
        public void Run(ListView lv, Button btnStart, Button btnStop, int manuId,string folder)
        {

            Load load = new Load(lv);
            List<CategoryModel> ListCate = new List<CategoryModel>();

            //  List<Page> ListPage = new List<Page>();
            string urlGoc = "http://dantri.com.vn/";


            ListCate = load.LoadCatesNoSub("http://dantri.com.vn/giai-tri.htm", "//ul[@id='ul_submenu']//li", urlGoc, manuId, "//a");


            int dem = 0;



            for (int i = 0; i < ListCate.Count(); i++)
            {





              
               

              

          
                 
                    List<NewsItemModel> ListDetail = new List<NewsItemModel>();
                    var load1 = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@class='nbmda mt3 clearfix']", "//div[@class='mr1']//h1//a", "", "//div[@class='mr1']//h1//a", urlGoc);

                  

                    ListDetail.AddRange(load1);

                    // phai chay dong for nguoc, de xet phan tu dau tien

                    for (int k = 0; k < ListDetail.Count(); k++)
                    {
                        var item = load.LoadNews(ListDetail[k], "//div[@class='fon34 mt3 mr2 fon43']", "//h2[@class='fon33 mt1']", manuId, 0, "//div[@class='fon34 mt3 mr2 fon43']//img", urlGoc,false);
                        if (item != null)
                        {
                            
                                dem++;

                                load.AddItemToListView(item);

                                pdb.SaveImgNormal(folder, item);

                         
                        }
                      



                  
                

                }



        }
    
         //   System.Windows.Forms.MessageBox.Show("\nThanh cong!Co " + dem + " tin moi");

            updateBtn.UpdateButtonStartAfterRun(btnStart);
            updateBtn.UpdateButtonStopAfterRun(btnStop);
            //updateBtn.UpdateAfterRun(btnStart,btnStop);
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");






        }
    }
}
